package com.mapbased.wfw;

import javax.servlet.*;

import com.mapbased.cc.*;
import java.io.IOException;
import java.io.File;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2004</p>
 *
 * <p>Company: mapbased.com</p>
 *
 * @author chy_hs(Baily)
 * @version 1.0
 */
public class GlobalRef
{
    public static final String PARA_PAGE_LOCATION="pageLocation";
    final PageLoader pageLoader=new PageLoader();
    final Config wfwConfig=new DefaultConfig();
    final ServletContext servletContext;

    private Object appContext;

    public final java.util.Timer timer;

    public GlobalRef(ServletContext sc)
    {
        timer=new java.util.Timer("Timer-"+sc.getServletContextName(),true);
        this.servletContext=sc;
        this.pageLoader.setGlobal(this);
        this.pageLoader.setDefaulPagePath(this.getPageLocation());
        this.wfwConfig.postLoad(this.pageLoader);

        this.pageLoader.init();
        this.initAppContext();
        this.servletContext.setAttribute(AppServlet.CTX_KEY_GLOBALREF,this);

    }

    private String getPageLocation()
    {
        String pageLocation=this.servletContext.getInitParameter(
              PARA_PAGE_LOCATION);

        if(pageLocation!=null&&pageLocation.trim().length()>0)
        {
            pageLocation=pageLocation.trim();

        }
        else
        {
            pageLocation="mapbased-gen";

        }

        String contextName=this.getServletContext().getServletContextName();
        if(!contextName.trim().equalsIgnoreCase("/"))
        {

            pageLocation = pageLocation +"/"+ contextName;
        }

        File plf=new File(pageLocation,"PageLocationRedir.txt");
        if(plf.exists())
        {
            try
            {
                String s=com.mapbased.util.Streaming.readStrFromStream(new
                      java.
                      io.FileInputStream(plf)).trim();

                File f=new File(s);
                if(f.exists()&&f.isDirectory())
                {
                    pageLocation=s;
                }
                else
                {
                    servletContext.log("Cannot set pageLocation ,not exist:"+s);
                }
            }
            catch(IOException ex)
            {
                servletContext.log(ex.toString(),ex);
            }

        }

        servletContext.log("Page location:"+pageLocation);
        return pageLocation;

    }

    public void destory()
    {
        this.timer.cancel();

        this.pageLoader.getPages().clear();

    }

    public PageLoader getPageLoader()
    {
        return pageLoader;
    }

    public Config getWfwConfig()
    {
        return wfwConfig;
    }

    public ServletContext getServletContext()
    {
        return servletContext;
    }

    public Object getAppContext()
    {
        return appContext;
    }



    private void initAppContext()
    {
        String cla=this.wfwConfig.getValue("appcontext");
        if(cla.trim().equals(""))
        {
            cla=this.wfwConfig.getValue("app-context");
        }
        try
        {

            Class c=cla.trim().equals("")?SpringAppContext.class:
                    Thread.currentThread().getContextClassLoader().loadClass(
                          cla.trim());
            try
            {
                this.appContext=c.getConstructor(new Class[]
                                                 {this.getClass()}).newInstance(new
                      Object[]
                      {this});
            }

            catch(NoSuchMethodException ex)
            {
                this.appContext=c.newInstance();
            }

        }
        catch(Exception t)
        {
            this.servletContext.log("Waring:Error while init appcontext",t);
            // throw new java.lang.InstantiationError(t.toString());
        }
    }
}
